package com.hyjx.framework.component.fastdfs;

import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageServer;

import java.io.File;
import java.io.FileInputStream;

/**
 * 分步式文件存储 测试类
 */
public class TestFileManager {
    
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String remoteFileName = "M00/00/00/rBYVFlfeCZmAb_VAAB4lUJAlGGY773.jpg";
//		String remoteFileName = "M00/00/00/rBYVFlfOVLGAfm8lACJch2mpqy4238.JPG";
//		String remoteFileName ="M00/00/00/rBYVFlfQvHqAVXFPAAAARtQAsM8727.txt";
//		getStorageServer();
//		upload();
//		getFile(remoteFileName);
        FileManager manager = new FileManager();
        manager.downloadFile(remoteFileName, "D:\\");
//		FileManager.deleteFile("group1","M00/00/00/rBYVFlfd_5-AYX8TAB4lUJAlGGY012.png-m");
    }
    
    public static void upload() throws Exception {
//		File content = new File("D:\\arab_pics\\5.png");
        File content = new File("D:\\IMG_0526.JPG");
        FileInputStream fis = new FileInputStream(content);
        byte[] file_buff = null;
        if (fis != null) {
            int len = fis.available();
            file_buff = new byte[len];
            fis.read(file_buff);
        }
        FastDFSFile file = new FastDFSFile("520", file_buff, "jpg");
        FileManager manager = new FileManager();
        String fileAbsolutePath = manager.upload(file);
        System.out.println(fileAbsolutePath);
        fis.close();
    }
    
    public static void getFile(String remoteFileName) throws Exception {
        FileManager manager = new FileManager();
        FileInfo file = manager.getFile("group1", remoteFileName);
        String sourceIpAddr = file.getSourceIpAddr();
        long size = file.getFileSize();
        System.out.println("ip:" + sourceIpAddr + ",size:" + size);
    }
    
    public static void getStorageServer() throws Exception {
        FileManager manager = new FileManager();
        StorageServer[] ss = manager.getStoreStorages("group1");
        
        for (int k = 0; k < ss.length; k++) {
            System.out.println(k
                    + 1
                    + ". "
                    + ss[k].getInetSocketAddress().getAddress()
                    .getHostAddress() + ":"
                    + ss[k].getInetSocketAddress().getPort()
                    + "\tStorePath" + ss[k].getStorePathIndex());
        }
    }
    
    public void getFetchStorages() throws Exception {
        FileManager manager = new FileManager();
        ServerInfo[] servers = manager.getFetchStorages("group1",
                "M00/00/00/wKgBm1N1-CiANRLmAABygPyzdlw073.jpg");
        
        for (int k = 0; k < servers.length; k++) {
            System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":"
                    + servers[k].getPort());
        }
    }
    
}
